package MyApp::Controller::Registadmin;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json encode_json);
use MyApp::Model::Translate qw(translateSbuId translateUserRole);


sub show_admin_account {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	#下行为测试行
	#my $username = $self->param('user') || '';

	my $db = $self->mysql->db;
	my $account_list = $db->query('select * from admin_account')->hashes;

	my @array = @{$account_list};

	#  for my $key (@array) {
	#    $key->{sbu_id} = &translateSbuId($key->{sbu_id});
	#    $key->{userrole} = &translateUserRole($key->{userrole});
	#  }

	$self->render(json => $account_list);
}


sub add_admin_account {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	#下行为测试行
	#my $username = $self->param('user') || '';


	my $db = $self->mysql->db;

	my $data = $self->req->body; #获取post的json值

	my $hash = decode_json $data; #JSON值转换为perl的散列

	my $username = $hash->{username};
	my $name = $hash->{name};
	my $sbu_id=$hash->{sbu_id};
	my $department=$hash->{department};
	my $password=$hash->{password};
	my $up_admin=$hash->{up_admin};
	my $userrole=$hash->{userrole};
	my $adminrole=$hash->{adminrole};
	$db->query('insert into admin_account (username,name,sbu_id,department,password,userrole,up_admin,enbale,adminrole) values (?,?,?,?,?,?,?,1,?)',$username,$name,$sbu_id,$department,$password,$userrole,$up_admin,$adminrole) or $self->render(json => {success => 0});
	$self->render(json => { success => 1 });

}


sub edit_admin_account {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	#下行为测试行
	#my $username = $self->param('user') || '';


	my $db = $self->mysql->db;

	my $data = $self->req->body; #获取post的json值

	my $hash = decode_json $data; #JSON值转换为perl的散列

	my $username = $hash->{username};
	my $name = $hash->{name};
	my $sbu_id=$hash->{sbu_id};
	my $department=$hash->{department};
	my $password=$hash->{password};
	my $up_admin=$hash->{up_admin};
	my $userrole=$hash->{userrole};
	my $adminrole=$hash->{adminrole};
	my $enable=$hash->{enbale};
	$db->query('update admin_account set name=?,sbu_id=?,department=?,password=?,userrole=?,up_admin=?,enbale=?,adminrole=? where username=?',$name,$sbu_id,$department,$password,$userrole,$up_admin,$enable,$adminrole,$username) or $self->render(json => {success => 0});
	$self->render(json => { success => 1 });

}


sub get_id_values {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	#下行为测试行
	#my $username = $self->param('user') || '';

	my $db = $self->mysql->db;
	my $sbu_ids = $db->query('select * from sbulist')->hashes;
	my $userrole = $db->query('select * from userrole')->hashes;

	#my %hash;

	#$hash{sbuid} = $sbu_ids;
	#$hash{userrole} = $userrole;

	$self->render(json => { sbuid => $sbu_ids,userrole => $userrole });
}

sub get_adminlist {
	my $self = shift;

	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	#下行为测试行
	#my $username = $self->param('user') || '';

	my $db = $self->mysql->db;

	my $hashes = $db->query('select username,name from admin_account')->hashes;

	$self->render(json => $hashes);
}

sub get_pass_info {
	my $self = shift;

	my $db = $self->mysql->db;

	my $user = $self->session('user');

	my $hash = $db->query('select username,name,department from admin_account where username=?',$user)->hash;

	$self->render(json => $hash);
}

sub update_admin_info {
	my $self = shift;

	my $db = $self->mysql->db;

	my $user = $self->session('user');

	my $data = $self->req->body;
	my $hash = decode_json $data;

	my $name = $hash->{name};
	my $department = $hash->{department};
	my $pass = $hash->{newpass};

    if ($pass) {
		$db->query('update admin_account set name=?,department=?,password=? where username=?',
	           $name,$department,$pass,$user);
	} else {
		$db->query('update admin_account set name=?,department=? where username=?',
	           $name,$department,$user);
	}
	
	$self->render(json => {success => 1});
}

1;